package com.agame;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;

public class PicLayer
{
	public final static int DIR_0 = 0;
	public final static int DIR_90 = 1;
	public final static int DIR_180 = 2;
	public final static int DIR_270 = 3;
	public final static int DIR_MOD = 4;
	private int mDir;
	
	public int getDir()
	{
		return mDir;
	}
	
	public final static int TYPE_PENDDING = 0;
	public final static int TYPE_PAINTING = 1;
	private int mType;
	
	
	public PicLayer(String path, boolean randomDir)
	{
		mBitmap = BitmapFactory.decodeFile(path);
		if(randomDir)
			mDir = Utils.randomInt(0, DIR_MOD);
		else
			mDir = 0;
	}
	
	public void setType(int type)
	{
		mType = type;
	}
	
	public void setRect(float left, float top, float right, float bottom)
	{
		if(mRect == null)
			mRect = new RectF(left, top, right, bottom);
		else
			mRect.set(left, top, right, bottom);
	}
	
	public void setRect(RectF rect)
	{
		if(mRect == null)
			mRect = new RectF(rect);
		else
			mRect.set(rect);
	}
	
	private Bitmap mBitmap = null;
	public Bitmap getBitmap()
	{
		return mBitmap;
	}
	
	private RectF mRect = null;
	public RectF getRect()
	{
		return mRect;
	}
	
	public void rotate()
	{
		mDir = (mDir + 1) % DIR_MOD;
	}
	
	public void draw(Canvas canvas)
	{
		Paint paint = new Paint();
		paint.setColor(Color.BLACK);
		paint.setStyle(Style.STROKE);
		canvas.drawRect(mRect, paint);
		canvas.save(Canvas.MATRIX_SAVE_FLAG);
		canvas.rotate(mDir * 90, mRect.centerX(), mRect.centerY());
		canvas.drawBitmap(mBitmap, null, mRect, null);
		canvas.restore();
	}
	
	public void drawInRect(Canvas canvas, RectF rect)
	{
		Paint paint = new Paint();
		paint.setColor(Color.BLACK);
		paint.setStyle(Style.STROKE);
		canvas.drawRect(rect, paint);
		canvas.save(Canvas.MATRIX_SAVE_FLAG);
		canvas.rotate(mDir * 90, rect.centerX(), rect.centerY());
		canvas.drawBitmap(mBitmap, null, rect, null);
		canvas.restore();
	}
	
	public void release()
	{
		if(mBitmap != null && !mBitmap.isRecycled())
			mBitmap.recycle();
	}
}
